package com.jinduplan.planmanager.ui.activity.project

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.cxz.kotlin.baselibs.adapter.CustomRecyclerAdapter
import com.cxz.kotlin.baselibs.adapter.ViewHolder
import com.cxz.kotlin.baselibs.base.BaseMvpActivity
import com.cxz.kotlin.baselibs.rx.bus.BusEvent
import com.cxz.kotlin.baselibs.rx.bus.RxBus
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.DataProjectBean
import com.jinduplan.planmanager.bean.ProjectListBean
import com.jinduplan.planmanager.mvp.contract.ProjectUploadContract
import com.jinduplan.planmanager.mvp.presenter.ProjectUploadPresenter
import com.jinduplan.planmanager.utils.ResUtil
import kotlinx.android.synthetic.main.activity_project.*


class ProjectUploadActivity :
    BaseMvpActivity<ProjectUploadContract.View, ProjectUploadContract.Presenter>(),
    ProjectUploadContract.View {


    private var isFromMain: Boolean = false
    override fun createPresenter(): ProjectUploadContract.Presenter = ProjectUploadPresenter()

    private var projectList: ArrayList<ProjectListBean.DataBean>? = null

    override fun start() {
        isFromMain = intent.getBooleanExtra("is_from_main", false)
        projectList = ArrayList()
        recyclerView.adapter = object : CustomRecyclerAdapter<ProjectListBean.DataBean>(
            this, R.layout.item_project, projectList
        ) {
            override fun convert(holder: ViewHolder?, t: ProjectListBean.DataBean?, position: Int) {
                holder?.setText(R.id.tvAddress, t?.project_name)
                holder?.setText(R.id.tvAddressDetail, t?.project_addr)
                holder?.setText(R.id.tv_look, ResUtil.getS(R.string.project_upload_to))
                holder?.setVisible(R.id.tv_look, !isFromMain)
                holder?.setOnClickListener(R.id.tv_look, View.OnClickListener {
                    val intent = Intent(applicationContext, ProjectUploadDetailActivity::class.java)
                    intent.putExtra("project_id", t?.id)
                    startActivity(intent)

                })
                if (isFromMain) {
                    holder?.convertView?.setOnClickListener {
                        RxBus.get().send(BusEvent.ProjectEvent( projectList!![position].project_addr, projectList!![position].project_name,projectList!![position].id.toString()))
                        finish()

                    }
                }
            }

        }
        recyclerView.layoutManager = LinearLayoutManager(this)

        swipeRefreshLayout.setOnRefreshListener {

            mPresenter?.getProjectList(1, 500)
        }
    }

    override fun onResume() {
        super.onResume()
        mPresenter?.getProjectList(1, 500)
    }

    override fun initView() {
        super.initView()
        setStatusBarColor(ResUtil.getC(R.color.white))
        tvTitle.text = if (isFromMain) {
            ResUtil.getS(R.string.engineering_upload)
        } else {
            ResUtil.getS(R.string.engineering_choose)
        }
        back.setOnClickListener { finish() }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_project
    }

    override fun setProjectListData(projectData: ProjectListBean) {
        swipeRefreshLayout.isRefreshing = false
        projectList?.clear()
        projectList?.addAll(projectData.data)
        recyclerView.adapter?.notifyDataSetChanged()
    }
}